-- TCErrorHandler
-- Release版本的错误处理
function ErrorHandlerInit()
    __draw = draw
    __touched = touched
    __db = ""
    __paused = false
    __showingConsole = false
    buffer = ""
    bugReportState = BugReportNone
    draw = function()
        if __paused then
            background(0, 0, 0, 255)
        else
            local status, err = xpcall(__draw, __errorHandler)
            if status == false then
                __paused = true             
                showConsole = true
            end
        end
        if showConsole == true and __showingConsole == false then
            __showingConsole = true
            __oldKB = keyboard
            keyboard = __keyboard
            showKeyboard()
        elseif showConsole == false and __showingConsole == true then
            __showingConsole = false
            hideKeyboard()
            keyboard = __oldKB
        end
        if __paused == false and not isKeyboardShowing() and showConsole then
            showConsole = false
        elseif __paused == true then
            showConsole = true
        end
        if __showingConsole then
            noStroke()
            rectMode(CORNER)
            fill(0, 0, 0, 180)
            rect(0,0,WIDTH,HEIGHT)
            font("Inconsolata")
            fontSize(ConsoleFontSize)
            textAlign(LEFT)
            textMode(CORNER)
            textWrapWidth(WIDTH - 10)
            fill(255, 255, 255, 255)
            local estr = "很抱歉，TC遇到了错误，"
            if BugReportEnabled then
                if bugReportState == BugReportSucceeded then
                    estr = estr .. "错误报告已发送"
                elseif bugReportState == BugReportFailed then
                    estr = estr .. "错误报告发送失败，重试中"
                else
                    estr = estr .. "正在发送错误报告"
                end
            else
                estr = estr .. "我们建议您将截图发送给开发者"
            end
            estr = estr .. "\n错误信息：" .. __db .. "\n"
            local str = "Console 指令输入工具\n:> " .. buffer
            if __paused then
                str = estr .. str
                if BugReportEnabled and bugReportState ~= BugReportSucceeded then
                    local s = string.gsub(__db," ","%%20")
                    s = string.gsub(s,"\n","%%0d")
                    s = BugReportHTTP .. s
                    http.request(s, bugReportSuccess,bugReportFailed)
                end
            end
            local _,h = textSize(str)
            text(str, 5, HEIGHT - h - 5)
        end
    end
    touched = function(touch)
        if showConsole and not isKeyboardShowing() then
            showKeyboard()
        end
        if __paused == false then
            local status, err = xpcall(function() __touched(touch) end, __errorHandler)
            if status == false then
                __paused = true             
                showConsole = true
            end
        end
    end
    
    
    __errorHandler = function (e)
        __db = e .. "\n" .. __traceback() .. "\n\n" .. "local variables:\n"
        
        local a = 1
        while true do
            local name, value = debug.getlocal(2, a)
            if not name then break end
            if name ~= "(*temporary)" then
                if name == "self" and type(value) == "table" then
                    local st = value
                    value = tostring(st).."\n[["
                    for k,v in pairs(st) do
                        value = string.format("%s\n       %s = %s", value, tostring(k), tostring(v))
                    end
                    value = value.."\n]]"
                end
                __db = string.format("%s\n%s = %s", __db, name, tostring(value))
            end
            a = a + 1
        end
        if BugReportEnabled then
            local s = BugReportHTTP .. string.gsub(string.gsub(__db," ","%%20"),"\n","%%0d")
            http.request(s, bugReportSuccess,bugReportFailed)
            alert("很抱歉，TC遇到了一些问题，将自动上传错误报告到服务器","出错啦")
        else
            alert("很抱歉，TC遇到了一些问题，建议反馈给开发者","出错啦")
        end
    end
    
    res = function()
        __paused = false  
        __db = ""
        showConsole = false
        bugReportState = BugReportNone
    end
end


function bugReportSuccess(str)
    if str == "Success" then
        bugReportState = BugReportSucceeded
    end
end

function bugReportFailed()
    if bugReportState == bugReportNone then
        bugReportState = BugReportFailed
    end
end